package com.scrm.server.wx.cp.web;

import com.scrm.api.wx.cp.dto.BrSopCountCustomerDTO;
import com.scrm.api.wx.cp.dto.WxMsgTemplateCountCustomerDTO;
import com.scrm.common.log.annotation.Log;
import com.scrm.common.dto.BatchDTO;
import com.scrm.server.wx.cp.dto.*;
import com.scrm.server.wx.cp.service.IBrSopService;
import com.scrm.server.wx.cp.entity.BrSop;

import com.scrm.server.wx.cp.vo.BrSopExecuteDetailVO;
import com.scrm.server.wx.cp.vo.BrSopPushDetailVO;
import com.scrm.server.wx.cp.vo.BrSopRuleDetailVO;
import com.scrm.server.wx.cp.vo.BrSopVO;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scrm.common.constant.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * sop 控制器
 * @author ouyang
 * @since 2022-04-17
 */
@RestController
@RequestMapping("/brSop")
@Api(tags = {"sop"})
public class BrSopController {

    @Autowired
    private IBrSopService brSopService;


    @PostMapping("/pageList")
    @ApiOperation(value = "分页查询")
    @Log(modelName = "sop", operatorType = "分页查询")
    public R<IPage<BrSopVO>> pageList(@RequestBody @Valid BrSopPageDTO dto){
        return R.data(brSopService.pageList(dto));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据主键查询")
    @Log(modelName = "sop", operatorType = "根据主键查询")
    public R<BrSopVO> findById(@PathVariable(value = "id") String id){
        Assert.isTrue(StringUtils.isNotBlank(id), "sopID不能为空");
        return R.data(brSopService.findById(id));
    }


    @PostMapping(value = "/save")
    @ApiOperation(value = "新增")
    @Log(modelName = "sop", operatorType = "新增")
    public R<BrSop> save(@RequestBody @Valid BrSopSaveDTO dto){
        return R.data(brSopService.save(dto));
    }


    @PostMapping(value = "/update")
    @ApiOperation(value = "修改")
    @Log(modelName = "sop", operatorType = "修改")
    public R<BrSop> update(@RequestBody @Valid BrSopUpdateDTO dto){
        return R.data(brSopService.update(dto));
    }


    @PostMapping("/delete")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "string", paramType = "query")
    @ApiOperation(value = "删除")
    @Log(modelName = "sop", operatorType = "删除")
    public R<Void> delete(String id){
        Assert.isTrue(StringUtils.isNotBlank(id), "sopID不能为空");
        brSopService.delete(id);
        return R.success("删除成功");
    }


    @PostMapping("/batchDelete")
    @ApiOperation(value = "批量删除")
    @Log(modelName = "sop", operatorType = "批量删除")
    public R<Void> batchDelete(@RequestBody @Valid BatchDTO<String> dto){
        brSopService.batchDelete(dto);
        return R.success("删除成功");
    }

    @PostMapping(value = "/updateStatus")
    @ApiOperation(value = "修改状态")
    @Log(modelName = "sop", operatorType = "修改状态")
    public R<Void> updateStatus(@RequestBody @Valid BrSopUpdateStatusDTO dto){
        brSopService.updateStatus(dto);
        return R.success("修改成功");
    }

    @GetMapping("/pushDetail")
    @ApiOperation(value = "应用内推送时获取推送详情")
    @Log(modelName = "sop", operatorType = "应用内推送时获取推送详情")
    public R<BrSopPushDetailVO> pushDetail(@RequestParam(value = "ruleId") String ruleId, @RequestParam(value = "executeAt") String executeAt,
            @RequestParam(value = "staffId") String staffId, @RequestParam(value = "jobId") Integer jobId, @RequestParam(value = "extCorpId") String extCorpId){
        Assert.isTrue(StringUtils.isNotBlank(ruleId), "规则ID不能为空");
        Assert.isTrue(executeAt!=null, "执行时间不能为空");
        Assert.isTrue(StringUtils.isNotBlank(staffId), "当前员工id不能为空");
        Assert.isTrue(jobId!=null, "jobId不能为空");

        return R.data(brSopService.getPushDetail(ruleId, executeAt, staffId, jobId, extCorpId));
    }

    @GetMapping("/executeDetail")
    @ApiOperation(value = "根据sopId查询规则执行详情")
    @Log(modelName = "sop", operatorType = "根据sopId查询规则执行详情")
    public R<List<BrSopRuleDetailVO>> executeDetail(@RequestParam(value = "sopId") String sopId){
        Assert.isTrue(StringUtils.isNotBlank(sopId), "sopID不能为空");
        return R.data(brSopService.getExecuteDetail(sopId));
    }

    @PostMapping("/updateSendStatus")
    @ApiOperation(value = "更新发送状态")
    @Log(modelName = "sop", operatorType = "更新发送状态")
    public R<Void> updateSendStatus(@RequestBody @Valid BrSopUpdateSendStatusDTO dto) {
        brSopService.updateSendStatus(dto);
        return R.success("修改成功");
    }

    @PostMapping("/remind")
    @ApiOperation(value = "提醒发送")
    @Log(modelName = "sop", operatorType = "提醒发送")
    public R<Void> remind(@RequestBody @Valid BrSopRemindDTO dto){
        brSopService.remind(dto);
        return R.success();
    }

    @PostMapping("/sendDetail")
    @ApiOperation(value = "查询发送详情")
    @Log(modelName = "sop", operatorType = "查询具体发送详情")
    public R<List<BrSopExecuteDetailVO>> sendDetail(@RequestBody @Valid SendDetailQueryDTO dto){
        return R.data(brSopService.getSendDetail(dto));
    }

    @PostMapping("/countCustomer")
    @ApiOperation(value = "统计符合条件的客户数量")
    @Log(modelName = "sop", operatorType = "统计符合条件的客户数量")
    public R<Integer> countCustomer(@RequestBody @Valid BrSopCountCustomerDTO dto){
        return R.data(brSopService.countCustomer(dto));
    }
}
